// 自荐指标
var GoalSelf = Ext.data.Record.create([{
	name : 'id',
	mapping : 'id',
	type : 'int'
}, {
	name : 'orgId',
	mapping : 'orgId',
	type : 'int'
},{
	name : 'content',
	mapping : 'content',
	type : 'string'
},{
	name : 'point',
	mapping : 'point',
	type : 'float'
},{
	name : 'spec',
	mapping : 'spec',
	type : 'string'
}, {
	name : 'ordering',
	mapping : 'ordering',
	type : 'float'
}, {
	name : 'memo',
	mapping : 'memo',
	type : 'string'
},{
	name : 'checkOrgId',
	mapping : 'checkOrgId',
	type : 'int'
},{
	name : 'year',
	mapping : 'year',
	type : 'int'
},{
	name : 'auditManId',
	mapping : 'auditManId',
	type : 'int'
},{
	name : 'auditOrgId',
	mapping : 'auditOrgId',
	type : 'int'
},{
	name : 'auditResult',
	mapping : 'auditResult',
	type : 'string'
},{
	name : 'auditContent',
	mapping : 'auditContent',
	type : 'string'
},{
	name : 'type',
	mapping : 'type',
	type : 'string'
}]);
var sm = new Ext.grid.RowNumberer();
var cm_goalself = new Ext.grid.ColumnModel([
	new Ext.grid.RowNumberer({header : '序号', width : 40, dataIndex: 'id'}),
	{id: 'orgId',header: "申报部门", width: 100, hidden: true, dataIndex: 'orgId'},
	{id: 'type',header: "指标类型", width: 80, sortable: true, dataIndex: 'type',renderer : fun_getGoalSelfType},
	{id: 'content',header: "指标内容", width: 100, sortable: true, dataIndex: 'content'},
	{id: 'spec',header: "评分标准", width: 100, sortable: true, dataIndex: 'spec'},
	{id: 'point',header: "分值", width: 100, sortable: true, dataIndex: 'point'},
	{id: 'ordering',header: "排序", width: 100, hidden: true, dataIndex: 'ordering'},
	{id: 'memo',header: "备注", width: 100, hidden: true, dataIndex: 'memo'},
	{id: 'checkOrgId',header: "考核责任单位", width: 100, sortable: true, dataIndex: 'checkOrgId',
		renderer : function(v) {
				var ret = '';
				var size = ds_org_select.getCount();
				for (var i = 0; i < size; i++) {
					var record = ds_org_select.getAt(i);
					var id = record.get('id');
					if (id == v) {
						ret = record.get('name');
						break;
					}
				}
				return (ret == '' || ret == null) ? v : ret;
			}},
    {id: 'year',header: "考核年度", width: 120, dataIndex: 'year'},
	{id: 'auditManId',header: "审核人", width: 100, hidden: true, dataIndex: 'auditManId'},
	{id: 'auditOrgId',header: "审核部门", width: 100, sortable: true, dataIndex: 'auditOrgId',
		renderer : function(v) {
				var ret = '';
				var size = ds_org_select.getCount();
				for (var i = 0; i < size; i++) {
					var record = ds_org_select.getAt(i);
					var id = record.get('id');
					if (id == v) {
						ret = record.get('name');
						break;
					}
				}
				return (ret == '' || ret == null) ? v : ret;
			}},
	{id: 'auditResult',header: "审核结果", width: 300, sortable: false, dataIndex: 'auditResult',renderer: fun_getAuditResult},
    {id: 'auditContent',header: "审核意见", width: 80, sortable: false, dataIndex: 'auditContent'}
]);

cm_goalself.defaultSortable = false;
var window_add_goalself = new Ext.Window({
			title : '添加自荐指标',
			width : 480,
			height : 640,
			resizable : false,
			autoHeight : true,
			modal : true,
			closeAction : 'hide',
			listeners : {
				'hide' : function() {
					this.setTitle('添加自荐指标');
					Ext.getCmp("form_add_goalself").getForm().reset();
				}
			},
			items : [new Ext.FormPanel({
						id : 'form_add_goalself',
						labelWidth : 100,
						labelAlign : 'right',
						url : '/supervise/biz/goalself/saveGoalSelf.action',
						method : 'post',
						border : false,
						baseCls : 'x-plain',
						bodyStyle : 'padding:5px 5px 0',
						defaults : {
							anchor : '90%',
							width : 240,
							msgTarget : 'side' // 验证信息显示右边
						},
						defaultType : 'textfield',
						items : [{
								xtype : 'hidden',
								name : 'goalSelf.id'
							}, {
								xtype : 'combo',
								fieldLabel : '<span style="color: red">*&nbsp;</span>指标类型',
								hiddenName : 'goalSelf.type',
								allowBlank : false,
								readOnly : true,
								editable : false,
								store : new Ext.data.SimpleStore({
											data : data_goal_self_type,
											fields : ['text', 'value']
										}),
								displayField : 'text',
								valueField : 'value',
								value : 'add',
								mode : 'local',
								triggerAction : 'all',
								emptyText : '请选择类型'
							},{
								xtype : 'hidden',
//								fieldLabel : '申报部门',
								name : 'goalSelf.orgId',
								value : user.orgId
							}, {
								xtype : 'textarea',
								fieldLabel : '<span style="color: red">*&nbsp;</span>指标内容',
								name : 'goalSelf.content',
								height : 200,
								allowBlank : false
							}, {
								xtype : 'numberfield',
								fieldLabel : '<span style="color: red">*&nbsp;</span>分值',
								name : 'goalSelf.point',
								allowBlank : false,
								maxLength : 20
							}, {
								xtype : 'textarea',
								fieldLabel : '<span style="color: red">*&nbsp;</span>评分标准',
								name : 'goalSelf.spec',
								allowBlank : false
							},{
								xtype : 'combo',
								fieldLabel : '<span style="color: red">*&nbsp;</span>考核责任单位',
								hiddenName : 'goalSelf.checkOrgId',
								valueField : 'id',
								displayField : 'name',
								mode : 'remote',
								store : ds_org_select,
								selectOnFocus : true,
								editable : false,
								allowBlank : false,
								triggerAction : 'all',
								loadingText : '加载中...',
								emptyText : '请选择部门',
								listeners : {
									'select' : function(combo, record, index) {
										var frm = Ext.getCmp("form_add_goalself").getForm();
										frm.findField('goalSelf.checkOrgId').setValue(record.data.id);
									}
								}
							
							}, {
								xtype : 'combo',
								fieldLabel : '<span style="color: red">*&nbsp;</span>考核年度',
								hiddenName : 'goalSelf.year',
								value: check_year,
								mode : 'local',
								store : store_year,
								valueField : 'value',
								displayField : 'text',
								selectOnFocus : true,
								editable : false,
								allowBlank : false,
								readOnly : true,
								triggerAction : 'all',
								emptyText : '请选择考核年度'
							}, { 
								xtype : 'combo',
								fieldLabel : '<span style="color: red">*&nbsp;</span>审核部门',
								hiddenName : 'goalSelf.auditOrgId',
								valueField : 'id',
								displayField : 'name',
								mode : 'remote',
								store : ds_org_select,
								selectOnFocus : true,
								editable : false,
								allowBlank : false,
								triggerAction : 'all',
								loadingText : '加载中...',
								emptyText : '请选择部门',
								listeners : {
									'select' : function(combo, record, index) {
										var frm = Ext.getCmp("form_add_goalself").getForm();
										frm.findField('goalSelf.auditOrgId').setValue(record.data.id);
									}
								}
							
							}, {
								xtype : 'hidden',
								name : 'goalSelf.ordering'
							}, {
								xtype : 'hidden',
								name : 'goalSelf.memo'
							}, {
								xtype : 'hidden',
//								fieldLabel : '审核人',
								name : 'goalSelf.auditManId'
							}, {
								fieldLabel : '备注',
								name : 'goalSelf.memo',
								disabled : true,
								maxLength : 200
							}],
						buttonAlign : 'center',
						minButtonWidth : 60,
						buttons : [{
									text : '添加',
									handler : function(btn) {
										var frm = Ext.getCmp("form_add_goalself").getForm();
										if (frm.isValid()) {
											btn.disable();
											var cnfield = frm.findField('goalSelf.content');
											frm.submit({
														waitTitle : '请稍候',
														waitMsg : '正在提交表单数据,请稍候...',
														success : function(form, action) {
															var store = grid_goalself.getStore();
															var goalself = new GoalSelf({
																		id : action.result.id,
																		orgId : form.findField('goalSelf.orgId').getValue(),
																		content : cnfield.getValue(),
																		point : form.findField('goalSelf.point').getValue(),
																		ordering : form.findField('goalSelf.ordering').getValue(),
																		spec : form.findField('goalSelf.spec').getValue(),
																		ordering : form.findField('goalSelf.ordering').getValue(),
																		memo : form.findField('goalSelf.memo').getValue(),
																		checkOrgId : form.findField('goalSelf.checkOrgId').getValue(),
																		year : form.findField('goalSelf.year').getValue(),
																		auditManId : form.findField('goalSelf.auditManId').getValue(),
																		auditOrgId : form.findField('goalSelf.auditOrgId').getValue(),
																		//auditResult : form.findField('goalSelf.auditResult').getValue(),
																		//auditContent : form.findField('goalSelf.auditContent').getValue(),
																		type : form.findField('goalSelf.type').getValue()
																	});
															store.insert(0, [goalself]);
															btn.enable();
															form.reset();
															window_add_goalself.hide();
														},
														failure : function() {
															Ext.Msg.show({
																		title : '错误提示',
																		msg : '"' + cnfield.getValue() + '" ' + '名称可能已经存在!',
																		buttons : Ext.Msg.OK,
																		fn : function() {
																			cnfield.focus(true);
																			btn.enable();
																		},
																		icon : Ext.Msg.ERROR
																	});
														}
													});
										}
									}
								}, {
									text : '重置',
									handler : function() {
										Ext.getCmp("form_add_goalself").getForm().reset();
									}
								}, {
									text : '取消',
									handler : function() {
										window_add_goalself.hide();
									}
								}]
					})]
		});
		
var window_edit_goalself = new Ext.Window({
			title : '修改自荐指标',
			width : 480,
			height : 640,
			resizable : false,
			autoHeight : true,
			modal : true,
			closeAction : 'hide',
			listeners : {
				'hide' : function() {
					this.setTitle('修改自荐指标');
					Ext.getCmp("form_edit_goalself").getForm().reset();
				}
			},
			items : [new Ext.FormPanel({
						id : 'form_edit_goalself',
						labelWidth : 100,
						labelAlign : 'right',
						url : '/supervise/biz/goalself/saveGoalSelf.action',
						method : 'post',
						border : false,
						baseCls : 'x-plain',
						bodyStyle : 'padding:5px 5px 0',
						defaults : {
							anchor : '90%',
							width : 240,
							msgTarget : 'side' // 验证信息显示右边
						},
						defaultType : 'textfield',
						items : [{
								xtype : 'hidden',
								name : 'goalSelf.id'
							}, {
								xtype : 'combo',
								fieldLabel : '<span style="color: red">*&nbsp;</span>指标类型',
								hiddenName : 'goalSelf.type',
								allowBlank : false,
								readOnly : true,
								editable : false,
								store : new Ext.data.SimpleStore({
											data : data_goal_self_type,
											fields : ['text', 'value']
										}),
								displayField : 'text',
								valueField : 'value',
								value : 'add',
								mode : 'local',
								triggerAction : 'all',
								emptyText : '请选择类型'
							},{
								xtype : 'hidden',
//								fieldLabel : '申报部门',
								name : 'goalSelf.orgId',
								value : user.orgId
							}, {
								xtype : 'textarea',
								fieldLabel : '<span style="color: red">*&nbsp;</span>指标内容',
								name : 'goalSelf.content',
								height : 200,
								allowBlank : false
							}, {
								xtype : 'numberfield',
								fieldLabel : '<span style="color: red">*&nbsp;</span>分值',
								name : 'goalSelf.point',
								allowBlank : false,
								maxLength : 20
							}, {
								xtype : 'textarea',
								fieldLabel : '<span style="color: red">*&nbsp;</span>评分标准',
								name : 'goalSelf.spec',
								allowBlank : false
							},{
								xtype : 'combo',
								fieldLabel : '<span style="color: red">*&nbsp;</span>考核责任单位',
								hiddenName : 'goalSelf.checkOrgId',
								valueField : 'id',
								displayField : 'name',
								mode : 'remote',
								store : ds_org_select,
								selectOnFocus : true,
								editable : false,
								allowBlank : false,
								triggerAction : 'all',
								loadingText : '加载中...',
								emptyText : '请选择部门',
								listeners : {
									'select' : function(combo, record, index) {
										var frm = Ext.getCmp("form_add_goalself").getForm();
										frm.findField('goalSelf.checkOrgId').setValue(record.data.id);
									}
								}
							
							}, {
								xtype : 'combo',
								fieldLabel : '<span style="color: red">*&nbsp;</span>考核年度',
								hiddenName : 'goalSelf.year',
								value: check_year,
								mode : 'local',
								store : store_year,
								valueField : 'value',
								displayField : 'text',
								selectOnFocus : true,
								editable : false,
								allowBlank : false,
								readOnly : true,
								triggerAction : 'all',
								emptyText : '请选择考核年度'
							}, { 
								xtype : 'combo',
								fieldLabel : '<span style="color: red">*&nbsp;</span>审核部门',
								hiddenName : 'goalSelf.auditOrgId',
								valueField : 'id',
								displayField : 'name',
								mode : 'remote',
								store : ds_org_select,
								selectOnFocus : true,
								editable : false,
								allowBlank : false,
								triggerAction : 'all',
								loadingText : '加载中...',
								emptyText : '请选择部门',
								listeners : {
									'select' : function(combo, record, index) {
										var frm = Ext.getCmp("form_add_goalself").getForm();
										frm.findField('goalSelf.auditOrgId').setValue(record.data.id);
									}
								}
							
							}, {
								xtype : 'hidden',
								name : 'goalSelf.ordering'
							}, {
								xtype : 'hidden',
								name : 'goalSelf.memo'
							}, {
								xtype : 'hidden',
//								fieldLabel : '审核人',
								name : 'goalSelf.auditManId'
							}, {
								fieldLabel : '备注',
								name : 'goalSelf.memo',
								disabled : true,
								maxLength : 200
							} ],
						buttonAlign : 'center',
						minButtonWidth : 60,
						buttons : [{
									text : '修改',
									handler : function(btn) {
										var frm = Ext.getCmp("form_edit_goalself").getForm();
										if (frm.isValid()) {
											btn.disable();
											var cnfield = frm.findField('goalSelf.content');
											frm.submit({
														waitTitle : '请稍候',
														waitMsg : '正在提交表单数据,请稍候...',
														success : function(form, action) {
															var record = grid_goalself.getSelectionModel().getSelected();
															// set value
															record.set("orgId", frm.findField('goalSelf.orgId').getValue());
															record.set("content", frm.findField('goalSelf.content').getValue());
															record.set("point", frm.findField('goalSelf.point').getValue());
															record.set("spec", frm.findField('goalSelf.spec').getValue());
															record.set("ordering", frm.findField('goalSelf.ordering').getValue());
															record.set("memo", frm.findField('goalSelf.memo').getValue());
															record.set("checkOrgId", frm.findField('goalSelf.checkOrgId').getValue());
															record.set("year", frm.findField('goalSelf.year').getValue());
															record.set("auditManId", frm.findField('goalSelf.auditManId').getValue());
															record.set("auditOrgId", frm.findField('goalSelf.auditOrgId').getValue());
															//record.set("auditResult", frm.findField('goalSelf.auditResult').getValue());
															//record.set("auditContent", frm.findField('goalSelf.auditContent').getValue());
															record.set("type", frm.findField('goalSelf.type').getValue());
															record.commit();
															frm.reset();
															btn.enable();
															window_edit_goalself.hide();
														},
														failure : function() {
															Ext.Msg.show({
																		title : '错误提示',
																		msg : '"' + cnfield.getValue() + '" ' + '名称可能已经存在!',
																		buttons : Ext.Msg.OK,
																		fn : function() {
																			cnfield.focus(true);
																			btn.enable();
																		},
																		icon : Ext.Msg.ERROR
																	});
														}
													});
										}
									}
								}, {
									text : '取消',
									handler : function() {
										window_edit_goalself.hide();
									}
								}]
					})]
		});
var btn_add_goalself = new Ext.Button({
			text : '添加',
			iconCls : 'icon-add',
			handler : function() {
				window_add_goalself.show(this);
				window_add_goalself.center();
			}
		});
var btn_edit_goalself = new Ext.Button({
			text : '修改',
			iconCls : 'icon-edit',
			handler : function() {
				if (!grid_goalself.checkOne()) {
					return;
				}
				window_edit_goalself.show(this);
				window_edit_goalself.center();
				var record = grid_goalself.getSelectionModel().getSelected();
				var frm = Ext.getCmp("form_edit_goalself").getForm();
				// set value
				frm.findField('goalSelf.id').setValue(record.get("id"));
				frm.findField('goalSelf.orgId').setValue(record.get("orgId"));
				frm.findField('goalSelf.content').setValue(record.get("content"));
				frm.findField('goalSelf.point').setValue(record.get("point"));
				frm.findField('goalSelf.spec').setValue(record.get("spec"));
				frm.findField('goalSelf.ordering').setValue(record.get("ordering"));
				frm.findField('goalSelf.memo').setValue(record.get("memo"));
				frm.findField('goalSelf.checkOrgId').setValue(record.get("checkOrgId"));
				frm.findField('goalSelf.year').setValue(record.get("year"));
				frm.findField('goalSelf.auditManId').setValue(record.get("auditManId"));
				frm.findField('goalSelf.auditOrgId').setValue(record.get("auditOrgId"));
				//frm.findField('goalSelf.auditResult').setValue(record.get("auditResult"));
				//frm.findField('goalSelf.auditContent').setValue(record.get("auditContent"));
				frm.findField('goalSelf.type').setValue(record.get("type"));
			}
		});
var btn_del_goalself = new Ext.Button({
			text : '删除',
			iconCls : 'icon-del',
			handler : function() {
				var record = grid_goalself.getSelectionModel().getSelected();
				if (record) {
					Ext.Msg.confirm('确认删除', '你确定删除该条记录?', function(btn) {
								if (btn == 'yes') {
									Ext.Ajax.request({
												url : '/supervise/biz/goalself/deleteGoalSelf.action',
												params : {
													id : record.data.id
												},
												success : function() {
													grid_goalself.getStore().remove(record);
												},
												failure : function() {
													Ext.Msg.show({
																title : '错误提示',
																msg : '删除时发生错误!',
																buttons : Ext.Msg.OK,
																icon : Ext.Msg.ERROR
															});
												}
											});
								}
							});
				}
			}
		});
var btn_search_goalself = new Ext.Button({
	text : '查询',
	iconCls : 'icon-search',
	handler : function() {
		grid_goalself.render();
		ds_goalself.load();
	}
});

var ds_goalself = new Ext.data.Store({
	proxy : new Ext.data.HttpProxy({
		url : '/supervise/biz/goalself/findGoalSelfByPage.action'
	}),
//	autoLoad: true,
	reader : new Ext.data.JsonReader({
		root : 'root',
		successProperty :'success'
	}, [{
	name : 'id',
	type : 'int'
}, {
	name : 'orgId',
	type : 'int'
},{
	name : 'content',
	type : 'string'
},{
	name : 'point',
	type : 'float'
},{
	name : 'spec',
	type : 'string'
}, {
	name : 'ordering',
	type : 'float'
}, {
	name : 'memo',
	type : 'string'
},{
	name : 'checkOrgId',
	type : 'int'
},{
	name : 'year',
	type : 'int'
},{
	name : 'auditManId',
	type : 'int'
},{
	name : 'auditOrgId',
	type : 'int'
},{
	name : 'auditResult',
	type : 'string'
},{
	name : 'auditContent',
	type : 'string'
},{
	name : 'type',
	type : 'string'
}])
});

var grid_goalself = new Ext.grid.GridPanel({
//			title : '自荐指标',
			iconCls : 'icon-grid',
			loadMask : {
				msg : '数据加载中...'
			},
			viewConfig : {
				forceFit : true,
				autoScroll : true
			},
			region : 'center',
			cm : cm_goalself,
			sm : new Ext.grid.RowSelectionModel({
						singleSelect : true
					}),
			store : ds_goalself,
			autoExpandColumn : 'symbol',
			tbar : [btn_add_goalself, '-', btn_edit_goalself, '-', btn_del_goalself],
			bbar : new Ext.PagingToolbar({
						pageSize : 20,
						store : ds_goalself,
						displayInfo : false,
						displayMsg : '第 {0} - {1} 条  共 {2} 条',
						refreshText : "刷新",
						emptyMsg : "没有记录"
					}),
			// 检测至少选择一个
			checkOne : function() {
				var selections = this.getSelectionModel().getSelections();
				if (selections.length == 0) {
					Ext.MessageBox.alert("提示", "请选择一条的记录！");
					return false;
				} else if (selections.length != 1) {
					Ext.MessageBox.alert("提示", "不能选择多行！");
					return false;
				}
				return true;
			},

			// 检测必须选择一个
			checkMany : function() {
				var selections = this.getSelectionModel().getSelections();
				if (selections.length == 0) {
					Ext.MessageBox.alert("提示", "请至少选择一条的记录！");
					return false;
				}
				return true;
			},
			listeners : {
				'render' : function(e) {
					ds_goalself.baseParams = {
						year : new Date().getFullYear(),
						selfStatus: 'apply'
					};
					ds_goalself.load({params : {
						start : 0,
						limit : 20
					}});
				}
			}

		});
		
var qpanel_goalself = new Ext.Panel({
//	title : '<h3 style="color:Green">请输入查询条件</h3>',
	height : 150,
	region : "north",
	baseCls:"x-plain",
	style:"padding:5px",
	layout:"column",
	items:[{
		columnWidth:0.6,
		layout:"form",
		frame : true,
		baseCls : 'x-plain',
		items : [new Ext.FormPanel({
			id: 'form_query_goalself',
			labelAlign : 'right',
			border : false,
			baseCls : 'x-plain',
			bodyStyle : 'padding:5px 5px 0',
			defaults : {
				anchor : '90%',
				width : 240,
				msgTarget : 'side' // 验证信息显示右边
			},
			defaultType : 'textfield',
			items:[{
					xtype : 'combo',
					fieldLabel : '考核年度',
					hiddenName : 'year',
					value: check_year,
					mode : 'local',
					store : store_year,
					valueField : 'value',
					displayField : 'text',
					selectOnFocus : true,
					editable : false,
					allowBlank : true,
					readOnly : true,
					triggerAction : 'all',
					emptyText : '请选择考核年度'
				}, {
					xtype : 'combo',
					fieldLabel : '审核结果',
					hiddenName : 'auditResult',
					allowBlank : true,
					readOnly : true,
					editable : false,
					store : new Ext.data.SimpleStore({
								data : data_audit_result,
								fields : ['text', 'value']
							}),
					displayField : 'text',
					valueField : 'value',
					mode : 'local',
					triggerAction : 'all',
					emptyText : '请选择审核结果'
				}, {
					xtype : 'combo',
					fieldLabel : '<span style="color: red">*&nbsp;</span>指标类型',
					hiddenName : 'type',
					allowBlank : false,
					readOnly : true,
					editable : false,
					store : new Ext.data.SimpleStore({
								data : data_goal_self_type,
								fields : ['text', 'value']
							}),
					displayField : 'text',
					valueField : 'value',
					mode : 'local',
					triggerAction : 'all',
					emptyText : '请选择类型'
				}, {
					fieldLabel : '考核内容',
					name : 'content'
				}],
			buttonAlign : 'center',
			minButtonWidth : 60,
			buttons : [{
						text : '查询',
						handler : function(btn) {
							var frm = btn.ownerCt.ownerCt.getForm();
							ds_goalself.baseParams = {
								year : frm.findField('year').getValue(),
								auditResult : frm.findField('auditResult').getValue(),
								type : frm.findField('type').getValue(),
								content : frm.findField('content').getValue(),
								selfStatus: 'apply'
							};
							ds_goalself.load({params : {
								start : 0,limit : 20
							}});
						}
					},{
						text : '重置',
						handler : function() { 
							this.ownerCt.ownerCt.getForm().reset();
						}
					}]
		})]
	}]
});
var query_goalself =  new Ext.Panel({
	frame : false,
	region : 'center',
	labelWidth : 65,
	title : '自荐指标申报',
	labelPad : 45,
	buttonAlign : 'center',
	layout : 'border',
	items : [qpanel_goalself, grid_goalself]
});			
var p_goalself = {
	id : 'goalself-panel',
	border : false,
	layout : 'border',
	items : [query_goalself]
};